package Week7;

import java.util.*;

public class Day37 {
}

//牛客:NC114 旋转字符串
class Solution1 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 旋转字符串
     * @param A string字符串
     * @param B string字符串
     * @return bool布尔型
     */
    public boolean solve (String A, String B) {
        // write code here
        if(A.length() != B.length()) return false;
        return (A + A).contains(B);
    }
}

//牛客:NC51 合并k个已排序的链表
class ListNode {
    int val;
   ListNode next = null;
    public ListNode(int val) {
     this.val = val;
   }
}
class Solution2 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     *
     * @param lists ListNode类ArrayList
     * @return ListNode类
     */
    public ListNode mergeKLists (ArrayList<ListNode> lists) {
        // write code here
        PriorityQueue<ListNode> q = new PriorityQueue<>((x , y) -> {
            return x.val - y.val;
        });

        for(ListNode head : lists){
            while(head != null){
                q.offer(head);
                head = head.next;
            }
        }

        ListNode dummy = new ListNode(-1);
        ListNode head = dummy;
        while(q.size() != 0){
            head.next = q.poll();
            head = head.next;
        }
        if(q.size() == 0){
            head.next = null;
        }

        return dummy.next;
    }
}

//牛客:DP18 滑雪
class Main1 {
    public static int N = 101;
    public static int n , m;
    public static int[][] arr = new int[N][N];
    public static int[][] dp = new int[N][N];
    public static int[] dx = {0,0,1,-1};
    public static int[] dy = {1,-1,0,0};

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                arr[i][j] = in.nextInt();
            }
        }
        int ret = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                ret = Math.max(ret , dfs(i,j));
            }
        }
        System.out.println(ret);
    }

    public static int dfs(int i , int j){
        if(dp[i][j] != 0) return dp[i][j];

        int len = 1;
        for(int k = 0; k < 4; k++){
            int x = dx[k] + i , y = dy[k] + j;
            if(x >= 0 && x < n && y >= 0 && y < m && arr[x][y] < arr[i][j]){
                len = Math.max(len , dfs(x , y) + 1);
            }
        }
        dp[i][j] = len;
        return len;
    }
}